package com.cloud.service.impl;

import com.cloud.entity.PageInfo;
import com.cloud.entity.Warehouse;
import com.cloud.mapper.GoodsMapper;
import com.cloud.mapper.WarehouseMapper;
import com.cloud.service.WarehouseService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author .
 * created in 2020/1/16 18:38
 */

@Service
public class WarehouseServiceImpl implements WarehouseService {

    private GoodsMapper goodsMapper;
    private WarehouseMapper warehouseMapper;

    public WarehouseServiceImpl(WarehouseMapper warehouseMapper, GoodsMapper goodsMapper) {
        this.warehouseMapper = warehouseMapper;
        this.goodsMapper = goodsMapper;
    }

    @Override
    public int addWarehouse(Warehouse warehouse) {
        warehouse.setPrice(goodsMapper.findGoodsByName(warehouse.getGoods()).getPrice() * warehouse.getCount());
        return warehouseMapper.addWarehouse(warehouse);
    }

    @Override
    public PageInfo<Warehouse> findAllWarehouseByPage(PageInfo<Warehouse> info, String id) {
        if (StringUtils.isEmpty(id)) id = null;
        info.setCount(warehouseMapper.getAllWarehouseCount(id));
        info.setData(warehouseMapper.findAllByPage((info.getPage() - 1) * info.getLimit(), info.getLimit(), id));
        return info;
    }

}
